import axios from "axios";
import store from "../store/index";
import router from "../router/index";
import loading from "../components/loading/loading";
import Vue from "vue";
Vue.use(loading);

const instance = axios.create({
  baseURL: "https://api-hmugo-web.itheima.net/api",
  // timeout:0
});

instance.interceptors.request.use(
  (config) => {
    // config.headers.Authorazition = store.state.token;
    //加上token 还能加全局的loading
    Vue.$loading.show();

    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
//请求拦截

instance.interceptors.response.use(
  (res) => {
    // if (res.msg == "无效token") router.push("/login");
    // if (res.code == 404) router.push("/notFound");
    //错误编码字典的处理 具体的信息或者编码根据后台给的接口文档实现
    Vue.$loading.hide();

    return res;
  },
  (err) => {
    return Promise.reject(err);
  }
);
//响应拦截

export default instance;
